package edu.fafu.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Controller;

import edu.fafu.dao.IQuestionDao;
import edu.fafu.entity.Question;
import edu.fafu.util.HibernateSupport;
import edu.fafu.util.PageBean;
@Controller("questionDao")
public class QuestionDao extends HibernateSupport implements IQuestionDao{
	
	@Override
	public Object queryQuestion(int id) {
		String Hql = "from Question q where q.id="+id;
		return getListOne(Hql);
	}
	
	
	@Override
	public void addQuestion(Question q) {
		add(q);		
	}

	@Override
	public void updateQuestion(Question q) {
		update(q);
	}
	
	@Override
	public void deleteQuestion(int id) {
		String Hql = "from Question q where q.id="+id;
		delete((Question)getListOne(Hql));
	}
	
	@Override
	public PageBean queryAllQuestion(PageBean pb) {
		String Hql = "from Question";
		return getList(Hql,pb);
	}


	@Override
	public PageBean queryQuestion(String condition,PageBean pb) {
		String Hql = "from Question where content like"+"%"+condition+"%";
		return getList(Hql,pb);
	}


	@Override
	public PageBean queryAnswer(Question q,PageBean pageBean) {	
		String Hql = "from Question q left join fetch q.answers where q.id="+q.getId();
		return getList(Hql,pageBean);
	}


	public PageBean queryQuestionByUser(int userId, PageBean pageBean) {
		String Hql = "from Question q where q.userId="+userId;
		return getList(Hql,pageBean);
	}
	
	
	
}
